/*
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-03-22 12:12:49
 * @LastEditTime: 2023-02-28 11:08:27
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description:
 * @FilePath: \vue3-element-plus-web\src\utils\auth.js
 * @Copyright 版权声明
 */
import Cookies from 'js-cookie'
import { CACHE_MODE } from '@/config/index'

// 登录人的 TOKEN
const TokenKey = 'admin-token'
// 登录人的 类型
const UserTypeKey = 'admin-user-type'
// 自动登录信息（用户名、密码）
const loginInfo = 'admin-user-login-info'

export function getToken() {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.getItem(TokenKey)
    default:
      return Cookies.get(TokenKey)
  }
}

export function setToken(token) {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.setItem(TokenKey, token)
    default:
      return Cookies.set(TokenKey, token)
  }
}

export function removeToken() {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.removeItem(TokenKey)
    default:
      return Cookies.remove(TokenKey)
  }
}

export function getUserType() {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.getItem(UserTypeKey)
    default:
      return Cookies.get(UserTypeKey)
  }
}

export function setUserType(type) {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.setItem(UserTypeKey, type)
    default:
      return Cookies.set(UserTypeKey, type)
  }
}

export function removeUserType() {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.removeItem(UserTypeKey)
    default:
      return Cookies.remove(UserTypeKey)
  }
}

export function getLoginInfo() {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.getItem(loginInfo) ? JSON.parse(sessionStorage.getItem(loginInfo)) : {
        loginName: '',
        password: '',
        autoLogin: false
      }
    default:
      return Cookies.get(loginInfo) ? JSON.parse(Cookies.get(loginInfo)) : {
        loginName: '',
        password: '',
        autoLogin: false
      }
  }
}

export function setLoginInfo(data) {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.setItem(loginInfo, JSON.stringify(data))
    default:
      return Cookies.set(loginInfo, JSON.stringify(data))
  }
}

export function removeLoginInfo() {
  switch (CACHE_MODE) {
    case 'SESSION_STORAGE':
      return sessionStorage.removeItem(loginInfo)
    default:
      return Cookies.remove(loginInfo)
  }
}
